/**
 * 
 * @author Marina Polese
 *
 */

package capture;

import javax.swing.*;
import javax.swing.table.*;

import capture.analyzer.AbstractPacketAnalyzer;

import java.util.*;
import java.awt.*;

import jpcap.packet.*;

public class TablePacket extends JComponent {

	private static final long serialVersionUID = 497158696253896552L;
	private JDTableModel model;
	private TableSorter sorter;
	private Vector<TableView> views = new Vector<TableView>();
	private Captor captor;

	public TablePacket(CapturePanel parent, Captor captor){
		this.captor=captor;
		model = new JDTableModel();
		sorter = new TableSorter(model);
		JTable table = new JTable(sorter);
		sorter.addMouseListenerToHeaderInTable(table);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getSelectionModel().addListSelectionListener(parent);
		table.setDefaultRenderer(Object.class,new TableRenderer());
		JScrollPane tableView=new JScrollPane(table);

		setLayout(new BorderLayout());
		add(tableView,BorderLayout.CENTER);
	}

	public void fireTableChanged(){
		model.fireTableRowsInserted(captor.getPackets().size()-1, captor.getPackets().size()-1);
	}

	public void clear(){
		model.fireTableStructureChanged();
		model.fireTableDataChanged();
	}

	public void setTableView(AbstractPacketAnalyzer analyzer, String name, boolean set){
		if(set){
			views.addElement(new TableView(analyzer,name));
		}else{
			for(int i=0;i<views.size();i++){
				TableView view = (TableView)views.elementAt(i);
				if(view.analyzer == analyzer && view.valueName.equals(name))
					views.removeElement(view);
			}
		}
		model.fireTableStructureChanged();
	}

	String[] getTableViewStatus(){
		String[] status=new String[views.size()];

		for(int i=0;i<status.length;i++){
			TableView view = (TableView)views.elementAt(i);
			status[i] = view.analyzer.getProtocolName()+":"+view.valueName;
		}

		return status;
	}

	public class TableView{
		AbstractPacketAnalyzer analyzer;
		String valueName;

		TableView(AbstractPacketAnalyzer analyzer,String name){
			this.analyzer = analyzer;
			this.valueName = name;
		}
	}

	public class JDTableModel extends AbstractTableModel {

		private static final long serialVersionUID = 497158696253896552L;

		public int getRowCount(){
			return captor.getPackets().size();
		}

		public int getColumnCount(){
			return views.size()+1;
		}

		public Object getValueAt(int row,int column){
			if(captor.getPackets().size() <= row) return "";
			Packet packet = (Packet)(captor.getPackets().get(row));

			if(column == 0)	return new Integer(row);
			TableView view = (TableView)views.elementAt(column-1);

			if(view.analyzer.isAnalyzable(packet)){
				synchronized(view.analyzer){
					view.analyzer.analyze(packet);
					Object obj = view.analyzer.getValue(view.valueName);

					if(obj instanceof Vector)
						if(((Vector<?>)obj).size()>0)
							return ((Vector<?>)obj).elementAt(0);
						else
							return null;
					else
						return obj;
				}
			}else{
				return null;
			}
		}

		public boolean isCellEditable(int row,int column){
			return false;
		}

		public String getColumnName(int column){
			if(column == 0) 
				return "No.";
			return ((TableView)views.elementAt(column-1)).valueName;
		}
	}

	public TableSorter getSorter() {
		return sorter;
	}

	public void setSorter(TableSorter sorter) {
		this.sorter = sorter;
	}
}
